<?php
class Mod_account extends CI_Model {

	/*
	 Attempts to login user and set session. Returns boolean based on outcome.
	 */
	function login($email, $password) {
		$query = $this -> db -> get_where(PREFIX_TABLE . 'product_owners', array('email' => $email, 'password' =>sha1($password), 'status' => 1), 1);
		if ($query -> num_rows() == 1) {
			$row = $query -> row();
			$this -> session -> set_userdata('simple_authentication_id', $row -> product_owner_id);
			return true;
		}
		return false;
	}

	/*
	 Logs out a user by destorying all session data and redirect to login
	 */
	function logout() {
		$this -> session -> sess_destroy();
		redirect('account');
	}

	/*
	 Determins if a user is logged in
	 */
	function is_logged_in() {
		if($this -> session -> userdata('simple_authentication_id')){
			return TRUE;
		}else{
			return FALSE;
		}
		
	}

	/*
	 Gets information about the currently logged in user.
	 */
	function get_logged_in_user_info() {
		if ($this -> is_logged_in()) {
			return $this -> get_info($this -> session -> userdata('simple_authentication_id'));
		}

		return false;
	}

	
	/*
	 Gets information about a particular user
	 */
	function get_info($user_id) {
		$this -> db -> from(PREFIX_TABLE . 'product_owners');
		//$this->db->join('people', 'people.person_id = users.person_id');
		$this -> db -> where(PREFIX_TABLE . 'product_owners.product_owner_id', $user_id);
		$query = $this -> db -> get();

		if ($query -> num_rows() == 1) {
			return $query -> row();
		} else {
			//Get empty base parent object, as $user_id is NOT an user
			$user_obj = parent::get_info(-1);

			//Get all the fields from user table
			$fields = $this -> db -> list_fields('product_owners');

			//append those fields to base parent object, we we have a complete empty object
			foreach ($fields as $field) {
				$user_obj -> $field = '';
			}

			return $user_obj;
		}
	}
	
	//mail control
	
	public function select_mail_count($user_id){
		$tbl_product_owners=PREFIX_TABLE."product_owners";
		$tbl_mail_user=PREFIX_TABLE."product_owner_mails";
		$tbl_mail_reply=PREFIX_TABLE."mail_replies";
		
		/*$this->db->select('product_owner_mail_id,subject,username,mail_modified_date,mail_status, COUNT(*) as numberReply');
		$this->db->from($tbl_mail_user);
		$this->db->join($tbl_product_owners,$tbl_product_owners.".product_owner_id = $tbl_mail_user.use_send_from_product_owner_id");
		$this->db->join($tbl_mail_reply,$tbl_mail_reply.".mail_reply_use_mail_id = $tbl_mail_user.product_owner_mail_id");
		$this->db->group_by('mail_reply_use_mail_id');
		$this->db->where('use_send_to_product_owner_id',$user_id);
		$this->db->or_where('is_reply=1 AND use_send_from_product_owner_id=',$user_id);*/
		$query=$this->db->query("SELECT product_owner_mail_id, subject, username, $tbl_mail_user.modified_date AS mail_modified_date, $tbl_mail_user.status AS mail_status, is_send_to_deleted, is_send_from_deleted, COUNT(*) as numberReply FROM (ctm_product_owner_mails) JOIN ctm_product_owners ON ctm_product_owners.product_owner_id = ctm_product_owner_mails.use_send_from_product_owner_id JOIN ctm_mail_replies ON ctm_mail_replies.use_mail_id = ctm_product_owner_mails.product_owner_mail_id WHERE use_send_to_product_owner_id = $user_id OR (is_reply='1' AND use_send_from_product_owner_id= $user_id) GROUP BY use_mail_id");
		
		return $query->num_rows();
	}
	public function select_mail_list_limit($start, $limit,$user_id){
		$tbl_product_owners=PREFIX_TABLE."product_owners";
		$tbl_mail_user=PREFIX_TABLE."product_owner_mails";
		$tbl_mail_reply=PREFIX_TABLE."mail_replies";
		
		/*$this->db->select('product_owner_mail_id,subject,username,mail_modified_date,mail_status, COUNT(*) as numberReply');
		$this->db->from($tbl_mail_user);
		$this->db->join($tbl_product_owners,$tbl_product_owners.".product_owner_id = $tbl_mail_user.use_send_from_product_owner_id");
		$this->db->join($tbl_mail_reply,$tbl_mail_reply.".mail_reply_use_mail_id = $tbl_mail_user.product_owner_mail_id");
		$this->db->group_by('mail_reply_use_mail_id');
		
		$this->db->where('use_send_to_product_owner_id',$user_id);
		$this->db->or_where('is_reply=1 AND use_send_from_product_owner_id=',$user_id);
		$this->db->limit($start,$limit);*/
		$query=$this->db->query("SELECT product_owner_mail_id, subject, username, $tbl_mail_user.modified_date, $tbl_mail_user.status AS mail_status, is_send_to_deleted, is_send_from_deleted, COUNT(*) as numberReply FROM (ctm_product_owner_mails) JOIN ctm_product_owners ON ctm_product_owners.product_owner_id = ctm_product_owner_mails.use_send_from_product_owner_id JOIN ctm_mail_replies ON ctm_mail_replies.use_mail_id = ctm_product_owner_mails.product_owner_mail_id WHERE use_send_to_product_owner_id = $user_id OR (is_reply='1' AND use_send_from_product_owner_id= $user_id) GROUP BY use_mail_id");
		
		return $query;
	}

	public function select_count_mail_active($user_id){
		$tbl_mail_user=PREFIX_TABLE."product_owner_mails";
		$query=$this->db->query("SELECT * FROM ctm_product_owner_mails INNER JOIN ctm_product_owners ON ctm_product_owner_mails.use_send_from_product_owner_id=ctm_product_owners.product_owner_id WHERE (use_send_to_product_owner_id = $user_id AND ctm_product_owner_mails.status = 1 ) OR (is_reply=1 AND use_send_from_product_owner_id= $user_id AND ctm_product_owner_mails.status = 1)");
		return  $query->num_rows();
		
	}
}
